# 2025.8.1
# harmonyOS_check.py 主要用于负责环境依赖检测函数
# harmonyOS_check.py
import os
import winreg
import json


# 1. 参数
DOWNLOAD_URL = "https://developer.huawei.com/consumer/cn/download/command-line-tools-for-hmos"


def get_devecostudio_info():
    """获取DevEco Studio安装信息"""
    reg_paths = [
        r"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall",
        r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
    ]
    try:
        for reg in reg_paths:
            with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, reg) as key:
                for i in range(winreg.QueryInfoKey(key)[0]):
                    sub = winreg.EnumKey(key, i)
                    with winreg.OpenKey(key, sub) as s:
                        try:
                            name = winreg.QueryValueEx(s, "DisplayName")[0]
                            if "DevEco Studio" in name:
                                loc = winreg.QueryValueEx(s, "InstallLocation")[0]
                                ver = winreg.QueryValueEx(s, "DisplayVersion")[0]
                                return {
                                    "DisplayName": name,
                                    "DisplayVersion": ver,
                                    "InstallLocation": loc
                                }
                        except (OSError, FileNotFoundError):
                            continue
    except Exception as e:
        return f"注册表读取失败: {e}"
    return "未找到 DevEco Studio"


def get_sdk_version_from_deveco(deveco_install_path):
    """从DevEco Studio安装路径获取SDK版本信息"""
    if not deveco_install_path:
        return {}

    json_path = os.path.join(deveco_install_path, "sdk", "default", "sdk-pkg.json")
    try:
        with open(json_path, encoding="utf-8") as f:
            data = json.load(f)
            d = data.get("data", {})
            return {
                "version": d.get("version", ""),
                "apiVersion": d.get("apiVersion", "")
            }
    except Exception as e:
        return {"error": f"读取失败: {e}"}


def get_environment_tools():
    """获取需要检测的环境工具列表"""
    return [
        ("Java", ["java"], "-version"),
        ("Python", ["python3", "python"], "--version"),
        ("Pip", ["pip3", "pip"], "--version"),
        ("Node.js", ["node"], "--version"),
        ("hdc", ["hdc"], "-v"),
        ("hvigor", ["hvigorw", "hvigor"], "-v"),
        ("ohpm", ["ohpm"], "--version"),
    ]

